package com.jsonal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsonal.entity.SysFileDt;
import com.jsonal.entity.SysFileDtBean;
import com.jsonal.entity.api.Article;
import com.jsonal.entity.api.ArticleBean;
import com.jsonal.entity.api.ArticleTags;
import com.jsonal.entity.api.Tags;
import com.jsonal.mapper.ArticleMapper;
import com.jsonal.mapper.ArticleTagsMapper;
import com.jsonal.mapper.SysFileDtMapper;
import com.jsonal.mapper.TagsMapper;
import com.jsonal.service.SystemService;
import com.jsonal.vo.Result;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: jsonal
 * @Description:
 * @Date: 2020/11/19 17:10
 * @Version: 1.0
 */
@Service
public class SystemServiceImpl implements SystemService {
    @Autowired
    private SysFileDtMapper sysFileDtMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleTagsMapper articleTagsMapper;
    @Autowired
    private TagsMapper tagsMapper;

    @Override
    public Result getCarouselList(SysFileDtBean sysFileDtBean) {
        Result result = new Result();
        List<SysFileDtBean> beans = new ArrayList<>();
        IPage page = new Page<SysFileDtBean>(sysFileDtBean.getPage(), sysFileDtBean.getPageSize());
        IPage files = sysFileDtMapper
            .selectPage(page, new QueryWrapper<SysFileDt>().eq("file_id", "carousel"));
        files.getRecords().forEach( item -> {
            SysFileDtBean bean = new SysFileDtBean();
            BeanUtils.copyProperties(item, bean);
            bean.setDelFlag(bean.getDelFlag());
            beans.add(bean);
        });
        result.setResults(beans);
        result.setSuccess(true);
        result.setObj(files);
        return result;

    }

    @Override
    public Result changeCarouselState(String fileDtId) {
        SysFileDt fileDt = sysFileDtMapper
            .selectOne(new QueryWrapper<SysFileDt>().eq("file_dt_id", fileDtId));
        if (fileDt.getDelFlag()) {
            fileDt.setDelFlag(false);
        } else {
            fileDt.setDelFlag(true);
        }
        sysFileDtMapper.update(fileDt, new QueryWrapper<SysFileDt>().eq("file_dt_id", fileDtId));
        return new Result(true, "操作成功");
    }

    @Override
    public Result getCarousel(SysFileDtBean sysFileDtBean) {
        Result result = new Result();
        List<SysFileDtBean> beans = new ArrayList<>();
        IPage page = new Page<SysFileDt>(sysFileDtBean.getPage(), sysFileDtBean.getPageSize());
        IPage files = sysFileDtMapper
            .selectPage(page, new QueryWrapper<SysFileDt>().eq("del_flag", false));
        files.getRecords().forEach(item -> {
            SysFileDtBean bean = new SysFileDtBean();
            BeanUtils.copyProperties(item, bean);
            bean.setDelFlag(bean.getDelFlag());
            beans.add(bean);
        });
        result.setResults(beans);
        result.setSuccess(true);
        return result;
    }


    /**
     * 根据 关键字 查找文章
     * @param keyword 关键字
     * @return 返回 关键字所包含的所有文章
     */
    @Override
    public Result search(String keyword) {
        Result result = new Result();
        List<Article> list = articleMapper
            .selectList(new QueryWrapper<Article>().like("title", keyword));
        if (list.size() > 0) {
            List<ArticleBean> beans = new ArrayList<>();
            list.forEach(item -> {
                ArticleBean bean = new ArticleBean();
                BeanUtils.copyProperties(item, bean);
                List<ArticleTags> tagsList = articleTagsMapper
                    .selectList(new QueryWrapper<ArticleTags>().eq("article_id", item.getId()));
                if (tagsList.size() > 0) {
                    List<String> tagString = new ArrayList<>();
                    tagsList.forEach(tag -> {
                        Tags tags = tagsMapper
                            .selectOne(new QueryWrapper<Tags>().eq("guid", tag.getTagsId()));
                        tagString.add(tags.getName());
                    });
                    bean.setTagsList(tagString);
                }
                beans.add(bean);
            });
            result.setSuccess(true);
            result.setMsg("查询成功");
            result.setResults(beans);
        }
        return result;
    }
}
